package com.maxlen.admin.entity.bo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.maxlen.admin.entity.Admin;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginDetailsBo implements UserDetails {

    private Admin admin;
    private List<String> role;

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(!role.isEmpty()){
            List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
            for (String s : role) {
                if(s != null){
                    grantedAuthorityList.add(new SimpleGrantedAuthority(s));
                }
            }
            return grantedAuthorityList;
        }
        return null;
    }

    @Override
    public String getPassword() {
        return admin.getPassword();
    }

    @Override
    public String getUsername() {
        return admin.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 账户是否锁定
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
